﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfRcp.Core.Common;
using WpfRcp.Core.Services;
using WpfRcp.Modules.Panels.Views;
using WpfRcp.Modules.Panels.Views.Presenters;
using WpfRcp.Core.Common.Workbench;
using WpfRcp.Modules.Panels.Properties;
using System.Reflection;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using WpfRcp.Modules.Panels.Views.Controls;
using WpfRcp.Core.Services.Commands;
using Microsoft.Practices.Composite.Regions;
using GlobalRegionNames = WpfRcp.Core.Globals.RegionNames;
using WpfRcp.Core.Globals;

namespace WpfRcp.Modules.Panels
{
    [Module(ModuleName = "PanelsModule")]
    internal sealed class PanelsModule : ModuleBase
    {
        private readonly IRegionManager m_RegionManager;
        private readonly IActionRegistry m_ActionRegistry;
        private readonly IMenuRegistry m_menuRegistry;
        private readonly IWorkbenchService m_WorkbenchService;

        public PanelsModule(
            IContainerFacade container,
            IRegionManager regionManager,
            IActionRegistry actionRegistry,
            IMenuRegistry menuRegistry,
            IWorkbenchService workbenchService)
            : base(container)
        {
            m_RegionManager = regionManager;
            m_ActionRegistry = actionRegistry;
            m_menuRegistry = menuRegistry;
            m_WorkbenchService = workbenchService;
        }

        private void PopulateRegions()
        {
            m_RegionManager.RegisterViewWithRegion(GlobalRegionNames.ToolbarRegion,
                () => Container.Resolve<ToolbarPresentationModel>().View);
        }

        protected override void RegisterViews()
        {
            // Register views
            Container.Register<IFirstTabView, FirstTabView>();
            Container.Register<ISecondTabView, SecondTabView>();
            Container.Register<IFileContentView, FileContentView>();

            // Register presenters
            Container.Register<FirstTabPresentationModel, FirstTabPresentationModel>(LifetimePolicy.Singleton);
            Container.Register<SecondTabPresentationModel, SecondTabPresentationModel>(LifetimePolicy.Singleton);
            Container.Register<FileContentPresentationModel, FileContentPresentationModel>();

            Container.Register<IToolbarView, ToolbarView>(LifetimePolicy.Singleton);
            Container.Register<ToolbarPresentationModel, ToolbarPresentationModel>(LifetimePolicy.Singleton);


            var firstTabPresentationModel = Container.Resolve<FirstTabPresentationModel>();
            var secondTabPresentationModel = Container.Resolve<SecondTabPresentationModel>();

            AttachViews2FirstTab(firstTabPresentationModel);
            AttachViews2SecondTab(secondTabPresentationModel);
            AttachMenus(firstTabPresentationModel);

            PopulateRegions();
        }

        private void AttachViews2FirstTab(FirstTabPresentationModel firstTabPresentationModel)
        {
            var WpfRcpItem = new ViewItem();
            WpfRcpItem.Visual = firstTabPresentationModel.View;
            WpfRcpItem.Name = WpfRcp.Modules.Panels.Properties.Resources.Displaying;

            m_WorkbenchService.Add(WpfRcpItem, DockStyle.RightPanel);
        }
        private void AttachViews2SecondTab(SecondTabPresentationModel secondTabPresentationModel)
        {
            var WpfRcpItem = new ViewItem();
            WpfRcpItem.Visual = secondTabPresentationModel.View;
            WpfRcpItem.Name = WpfRcp.Modules.Panels.Properties.Resources.Saving;

            m_WorkbenchService.Add(WpfRcpItem, DockStyle.RightPanel);
        }

        protected override ModuleInfoItem GetModuleInfo()
        {
            // Add about info
            var item = new ModuleInfoItem
            {
                Name = Resources.ModuleInfo_Name,
                HeaderInfo = Resources.ModuleInfo_Header,
                Authors = Resources.ModuleInfo_Authors,
                Description = Resources.ModuleInfo_Description,
                Version = string.Format(Resources.ModuleInfo_Version, Assembly.GetExecutingAssembly().GetName().Version, IntPtr.Size * 8),
                CopyrightNotice = Resources.ModuleInfo_Copyright,
                Rights = Resources.ModuleInfo_Rights
            };

            return item;
        }

        private void AttachMenus(FirstTabPresentationModel firstTabPresentationModel)
        {
            m_ActionRegistry.RegisterCommandCategory("WpfRcp");
            m_ActionRegistry.RegisterCommand("OpenFile",
                                 "WpfRcp",
                                 firstTabPresentationModel.OpenAndDisplayFileCommand);

            var group = m_menuRegistry.RegisterMenuItemGroup("FileManagement", ApplicationConstants.Menus.Level1.File);
            group.Text = Resources.MenuGroupFileManagement;
            m_menuRegistry.RegisterMenuItem("OpenFile",
                                  "FileManagement",
                                  "OpenFile")
                .SetPosition(10).SetText(WpfRcp.Modules.Panels.Properties.Resources.OpenFileMenuItem);
            m_menuRegistry.RegisterMenuItem("OpenFile2",
                                  "FileManagement",
                                  "OpenFile")
                .SetPosition(5).SetText(WpfRcp.Modules.Panels.Properties.Resources.OpenFileMenuItem + " 2");

            group = m_menuRegistry.RegisterMenuItemGroup("SubFileManagement", ApplicationConstants.Menus.Level1.File);
            group.Text = Resources.SubMenuGroupFileManagement;
            m_menuRegistry.RegisterMenuItem("OpenFile3",
                                  "SubFileManagement",
                                  "OpenFile")
                .SetPosition(10).SetText(WpfRcp.Modules.Panels.Properties.Resources.OpenFileMenuItem + " 3");

            m_menuRegistry.RegisterMenuItem("OpenFile4",
                      "SubFileManagement",
                      "OpenFile")
                .SetPosition(20).SetText(WpfRcp.Modules.Panels.Properties.Resources.OpenFileMenuItem + " 4");

            var tab = m_menuRegistry.RegisterMenuTab("NewCategory")
                .SetPosition(30);
            tab.Text = WpfRcp.Modules.Panels.Properties.Resources.NewCategory;

            group = m_menuRegistry.RegisterMenuItemGroup("NewCategory", "NewCategory");
            group.Text = Resources.NewCategory;

            m_menuRegistry.RegisterMenuItem("OpenFile5",
                                  "NewCategory",
                                  "OpenFile")
                .SetPosition(10).SetText(WpfRcp.Modules.Panels.Properties.Resources.OpenFileMenuItem);
            m_menuRegistry.RegisterMenuItem("OpenFile6",
                                  "NewCategory",
                                  "OpenFile")
                .SetPosition(5).SetText(WpfRcp.Modules.Panels.Properties.Resources.OpenFileMenuItem + " 2");

        }
    }

}
